# @file    : simple_react
# @time    : 2025/3/20
# @author  : yongpeng.yao
# @desc    :
import os

from langchain import hub
from langchain_openai import ChatOpenAI

os.environ['DASHSCOPE_API_KEY'] = "sk-279c68899b4b4b12abf531d1d42a1572"
os.environ['SERPAPI_API_KEY'] = 'fe984c0e9673eb389524aa8b02cf691fd454ce8f5b6ce0319b68ecfc03f3aa43'

prompt = hub.pull('hwchase17/react')
print(prompt)

llm = ChatOpenAI(
    model='deepseek-r1',
    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 如何获取API Key：https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)

from langchain_community.utilities import SerpAPIWrapper
from langchain_core.tools import Tool

search = SerpAPIWrapper()
tools = [
    Tool(
        name="Search",
        func=search.run,
        description="当大模型没有相关的知识时，用此工具进行搜索",
    )
]

# 导入create_react_agent函数
from langchain.agents import create_react_agent

agent = create_react_agent(llm, tools, prompt)
# 导入AgentExecutor
from langchain.agents import AgentExecutor

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "当前Agent最新研究进展是什么？"})
